package com.happy.study.controller;

import com.happy.study.entity.FileEntity;
import com.happy.study.entity.FollowEntity;
import com.happy.study.entity.UserEntity;
import com.happy.study.entity.bo.FollowBo;
import com.happy.study.servier.IFileService;
import com.happy.study.servier.IFollowService;
import com.happy.study.servier.IUserService;
import com.happy.study.util.ConstUtil;
import com.happy.study.util.ResultBody;
import com.happy.study.util.StateEnum;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping ( "/user" )
public class UserController {

    @Resource
    private IUserService iUserService;
    @Resource
    private IFollowService iFollowService;
    @Resource
    private IFileService iFileService;

    /**
     * 登录系统，根据用户信息查询用户是否存在
     * verification: 验证码，若不为空，则为邮箱登录
     *
     * @param userEntity
     * @return
     */
    @RequestMapping ( "/querySingle" )
    @ResponseBody
    public ResultBody<UserEntity> querySingle ( UserEntity userEntity, @RequestParam ( value = "verification", required = false ) String verification, HttpSession session ) {
        userEntity = iUserService.querySingle ( userEntity );
        if ( userEntity == null ) {
            return ResultBody.fail ( StateEnum.RESULT_NULL_EXCE );
        }
        if ( verification != null ) {
            String verCode = ( String ) session.getAttribute ( userEntity.getEmail () + ConstUtil.VERIFICATION );
            if ( ! verification.equals ( verCode ) ) {
                return ResultBody.fail ( StateEnum.VERIFICATION_EXCE );
            }
        }
        // 根据session里面的用户id获取关联到的file对象
        userEntity = iUserService.queryById ( userEntity );
        session.setAttribute ( "userEntity", userEntity );
        return ResultBody.success ( userEntity );
    }

    /**
     * 个人中心
     *
     * @param session
     * @return
     */
    @RequestMapping ( "/center" )
    public String center ( Model model, HttpSession session ) {
        UserEntity userEntity = ( UserEntity ) session.getAttribute ( "userEntity" );
        model.addAttribute ( "userEntity", userEntity );
        return "personInfo";
    }

    /**
     * 个人主页，查看其他好友信息
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping ( "/index" )
    public String index ( Model model, HttpSession session ) {
        UserEntity userEntity = ( UserEntity ) session.getAttribute ( "userEntity" );
        model.addAttribute ( "userEntity", userEntity );
        model.addAttribute ( "flag", false ); // 为了解决查看其他用户报错的问题,无实际意义
        return "person";
    }

    /**
     * 根据用户Id查询用户信息
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping ( "/{id}" )
    public String getUser ( @PathVariable int id, Model model, HttpSession session ) {
        UserEntity userEntity = new UserEntity ();
        userEntity.setId ( id );
        userEntity = iUserService.queryById ( userEntity );
        model.addAttribute ( "userEntity", userEntity );
        UserEntity user = ( UserEntity ) session.getAttribute ( "userEntity" );
        boolean flag = false;
        if ( user != null ) {
            FollowEntity followEntity = new FollowEntity ();
            followEntity.setUserId ( user.getId () );
            List<FollowBo> followBos = iFollowService.queryList ( followEntity );
            for ( FollowBo followBo : followBos ) {
                if ( followBo.getFriendId () == id ) {
                    flag = true;
                }
            }
        }
        model.addAttribute ( "flag", flag );
        return "person";
    }


    /**
     * 获取邮箱验证码
     *
     * @param userEntity
     * @return
     */
    @RequestMapping ( "/getVerification" )
    @ResponseBody
    public ResultBody<UserEntity> getVerification ( UserEntity userEntity, HttpSession session ) {
        String verification = iUserService.sendVerification ( userEntity );
        session.setAttribute ( userEntity.getEmail () + ConstUtil.VERIFICATION, verification ); // 暂存session。随着业务增大可放置redis提高效率
        return ResultBody.success ( userEntity );
    }

    /**
     * 修改个人信息
     *
     * @param userEntity
     * @param session
     * @return
     */
    @RequestMapping ( "/updateSingle" )
    @ResponseBody
    public ResultBody<UserEntity> saveSingle ( UserEntity userEntity, HttpSession session ) {
        UserEntity user = ( UserEntity ) session.getAttribute ( "userEntity" );
        if ( user == null ) {
            return ResultBody.fail ( StateEnum.REQUEST_POWER_EXCE );
        }
        userEntity.setId ( user.getId () );
        userEntity = iUserService.updateSingle ( userEntity );
        return ResultBody.success ( userEntity );
    }


    /**
     * 上传更新用户头像
     *
     * @param multipartFile
     * @param session
     * @return
     */
    @RequestMapping ( "/uploadIcon" )
    @ResponseBody
    public ResultBody<FileEntity> uploadIcon ( @RequestParam ( "file" ) MultipartFile multipartFile, HttpSession session ) {
        UserEntity userEntity = ( UserEntity ) session.getAttribute ( "userEntity" );
        FileEntity fileEntity = new FileEntity ();
        fileEntity.setUserId ( userEntity.getId () );
        fileEntity = iFileService.addSingle ( fileEntity, multipartFile );
        return ResultBody.success ( fileEntity );
    }

    @RequestMapping ( "/logout" )
    @ResponseBody
    public ResultBody<UserEntity> logout ( HttpSession session ) {
        session.removeAttribute ( "userEntity" );
        return ResultBody.success ();
    }


    /**
     * 从session里获取UserEntity，用于验证当前用户是否登录
     * @param session
     * @return
     */
    @RequestMapping ( "/getSessionUser" )
    @ResponseBody
    public ResultBody<UserEntity> getSessionUser ( HttpSession session ) {
        UserEntity userEntity = ( UserEntity ) session.getAttribute ( "userEntity" );
        if ( userEntity == null ) {
            return ResultBody.fail ( StateEnum.REQUEST_POWER_EXCE );
        }
        return ResultBody.success ();
    }

}
